import uix
import xtriui
import blue
import uthread
import draw
import util
import uicls

class StationNav(uicls.Container):
    __guid__ = 'form.StationNav'

    def init(self):
        self.looking = 1
        self.blockDisable = 0



    def Startup(self):
        return 



    def GetMenu(self):
        scene2 = sm.GetService('sceneManager').GetActiveScene2()
        (x, y,) = (eve.triapp.uilib.x, eve.triapp.uilib.y)
        if scene2:
            (projection, view, viewport,) = uix.GetFullscreenProjectionViewAndViewport()
            pick = scene2.PickObject(x, y, projection, view, viewport)
            if (pick and (pick.name == str(eve.session.shipid))):
                return self.GetShipMenu()



    def _OnMouseEnter(self, *args):
        if ((not self.blockDisable) and (not uicore.cmd.IsUIHidden())):
            uicore.layer.main.state = uix.UI_PICKCHILDREN



    def OnDropData(self, dragObj, nodes):
        sm.GetService('loading').StopCycle()
        if (len(nodes) == 1):
            node = nodes[0]
            if (node.Get('__guid__', None) not in ('xtriui.InvItem', 'listentry.InvItem')):
                return 
            if (eve.session.shipid == node.item.itemID):
                eve.Message('CantMoveActiveShip', {})
                return 
            if (node.item.categoryID == const.categoryShip):
                if node.item.singleton:
                    if not (node.item.ownerID == eve.session.charid):
                        eve.Message('CantDoThatWithSomeoneElsesStuff')
                        return 
                    sm.GetService('station').TryActivateShip(node.item)
            selsvc = sm.GetService('station').GetSvc()
            if ((selsvc is not None) and util.LocalSvcHasAttr(selsvc, 'OnStuffDropOnMainArea')):
                uthread.new(selsvc.OnStuffDropOnMainArea, nodes)



    def GetShipMenu(self):
        if eve.session.shipid:
            hangarInv = eve.GetInventory(const.containerHangar)
            hangarItems = hangarInv.List()
            for each in hangarItems:
                if (each.itemID == eve.session.shipid):
                    return sm.GetService('menu').InvItemMenu(each)

        return []



    def _OnDblClick(self, *args):
        if (eve.session.shipid and (sm.GetService('godma').GetItem(eve.session.shipid).groupID != const.groupCapsule)):
            uicore.cmd.OpenCargoHoldOfActiveShip()
        elif (eve.Message('AskActivateShip', {}, uix.YESNO, suppress=uix.ID_YES) == uix.ID_YES):
            sm.GetService('station').SelectShipDlg()



    def _OnMouseDown(self, *args):
        if settings.user.ui.Get('loadstationenv', 1):
            self.looking = 1
        if ((not self.blockDisable) and (not uicore.cmd.IsUIHidden())):
            uicore.layer.main.state = uix.UI_DISABLED
        self.cursor = uix.UICURSOR_DRAGGABLE
        eve.triapp.uilib.ClipCursor(0, 0, eve.triapp.uilib.desktop.width, eve.triapp.uilib.desktop.height)
        eve.triapp.uilib.SetCapture(self)



    def _OnMouseUp(self, button, *args):
        if ((not self.blockDisable) and (not uicore.cmd.IsUIHidden())):
            uicore.layer.main.state = uix.UI_PICKCHILDREN
        if settings.user.ui.Get('loadstationenv', 1):
            camera = sm.GetService('sceneManager').GetRegisteredCamera('default')
            if (camera is None):
                return 
            camera.interest = None
            camera.friction = 7.0
            if not eve.triapp.uilib.rightbtn:
                camera.rotationOfInterest.SetIdentity()
            if not eve.triapp.uilib.leftbtn:
                self.looking = 0
        self.cursor = None
        eve.triapp.uilib.UnclipCursor()
        if (eve.triapp.uilib.capture == self):
            eve.triapp.uilib.ReleaseCapture()



    def _OnMouseWheel(self, *args):
        if settings.user.ui.Get('loadstationenv', 1):
            camera = sm.GetService('sceneManager').GetRegisteredCamera('default')
            if (camera is None):
                return 
            if (camera.__typename__ == 'EveCamera'):
                camera.Dolly(((-eve.triapp.uilib.dz * 0.001) * abs(camera.translationFromParent.z)))
                camera.translationFromParent.z = sm.GetService('station').CheckCameraTranslation(camera.translationFromParent.z)



    def _OnMouseMove(self, *args):
        if (self.looking and settings.user.ui.Get('loadstationenv', 1)):
            lib = eve.triapp.uilib
            dx = lib.dx
            dy = lib.dy
            camera = sm.GetService('sceneManager').GetRegisteredCamera('default')
            if (camera is None):
                return 
            fov = camera.fieldOfView
            ctrl = lib.Key(uix.VK_CONTROL)
            if (lib.rightbtn and (not lib.leftbtn)):
                camera.RotateOnOrbit(((dx * fov) * 0.2), ((-dy * fov) * 0.2))
            if (lib.leftbtn and (not lib.rightbtn)):
                camera.OrbitParent(((dx * fov) * 0.2), ((-dy * fov) * 0.2))
            if (lib.leftbtn and lib.rightbtn):
                station = sm.GetService('station')
                camera.Dolly(((dy * 0.01) * abs(camera.translationFromParent.z)))
                camera.translationFromParent.z = station.CheckCameraTranslation(camera.translationFromParent.z)
                if ctrl:
                    camera.fieldOfView = ((-dx * 0.01) + fov)
                    if (camera.fieldOfView > 1.0):
                        camera.fieldOfView = 1.0
                    if (camera.fieldOfView < 0.1):
                        camera.fieldOfView = 0.1
                else:
                    camera.OrbitParent(((dx * fov) * 0.2), 0.0)
        else:
            self.cursor = None




